﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpriteGame.Helpers;

namespace SpriteGame.UI
{
    interface IFocusable
    {
        bool HasFocus
        {
            get;
            set;
        }
        void UpdateInput(GameTime _time);
    }

    interface IUserControl
    {
        Rectangle Bounds
        {
            get;
            set;
        }
        void Render(SpriteBatch _batch, Vector2 _parentPosition);
    }

    public abstract class UIPanel:IUserControl
    {
        public static SpriteFont GlobalFont;

        protected Rectangle boundsRect;
        protected Vector2 position;
        protected int width;
        protected int height;

        protected bool drag = false;

        protected List<UIPanel> children;
        protected UIPanel parent;
        public UIPanel Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        public UIPanel GetCollider(Point _point)
        {
            foreach (UIPanel p in children)
            {
                if (p.Bounds.Contains(_point))
                {
                    UIPanel target = p.GetCollider(_point);
                    return p;
                }
            }

            LeftClick();
            return this;
        }

        public event ClickHandler Clicked;
        public delegate void ClickHandler(UIPanel _owner);

        protected virtual void LeftClick()
        {
            if (Clicked != null)
            {
                Clicked.Invoke(this);
            }
        }

        public void FireClick()
        {
            if (Clicked != null)
            {
                Clicked.Invoke(this);
            }
        }

        public delegate void OnClick();

        bool isVisible = true;

        public bool IsVisible
        {
            get { return isVisible; }
            set { isVisible = value; }
        }
        
        public UIPanel(int _left, int _top, int _width, int _height)
        {
            children = new List<UIPanel>();
            boundsRect = new Rectangle(_left, _top, _width, _height);
            position = new Vector2(_left, _top);
            width = _width;
            height = _height;
        }

        public virtual void SetSize(int _width, int _height)
        {
            width = _width;
            height = _height;
        }

        public virtual void SetPosition(Vector2 _position)
        {
            position = _position;
        }

        public virtual Vector2 GetPosition()
        {
            return position;
        }

        public virtual void AddChild(UIPanel _child)
        {
            children.Add(_child);
        }

        public virtual void DeleteChild(UIPanel _child)
        {
            children.Remove(_child);
        }

        public virtual void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {

        }

        public virtual void Update(Vector2 _parentPosition)
        {
            foreach (UIPanel child in children)
            {
                child.Update(position - _parentPosition);
            }

            if (InputHandler.LeftRelease())
            {
                drag = false;
            }

            if (drag)
            {
                position = InputHandler.MousePosition - _parentPosition;
            }
        }

        public virtual void Render(SpriteBatch _batch, Vector2 _parentPosition)
        {
            Vector2 renderPosition = _parentPosition + position;
            boundsRect.X = (int)(renderPosition.X);
            boundsRect.Y = (int)(renderPosition.Y);
            
            if(isVisible)
                RenderContent(_batch, boundsRect);
            
            foreach (UIPanel child in children)
            {
                child.Render(_batch, renderPosition);
            }
        }

        public Rectangle Bounds
        {
            get
            {
                return boundsRect;
            }
            set
            {
                boundsRect = value;
            }
        }
    }
}
